package com.beamfield.controller.manage.mixstation;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.MixStation;
import com.beamfield.modelVo.FileInfoVo;
import com.beamfield.service.manage.lab.LabService;
import com.beamfield.service.manage.mixstation.MixBuildService;
import com.beamfield.service.manage.sys.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 拌和站建设
 */
@Controller
@RequestMapping(value = "/cms/mixstation/build")
public class MixBuildController extends BaseController{

    private final MixBuildService mixBuildService;


    @Autowired
    public MixBuildController(MixBuildService mixBuildService) {
        this.mixBuildService = mixBuildService;
    }

    @Autowired
    private LabService labService;

    @Value("${session.beam}")
    private String beam;
    @Value("${session.user}")
    private String user;


    @RequestMapping(value = "/organization")
    public ModelAndView oranization(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/institution")
    public ModelAndView institution(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/operator")
    public ModelAndView operator(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/adjustment")
    public ModelAndView adjustment(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/editInstitution")
    public ModelAndView editInstitution(){
        return new ModelAndView();
    }

    /**
     * 获取拌和站数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/readMixStationData",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readMixStationData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        MixStation mixStationData = mixBuildService.getMixStationData(beamId);
        return new ApiResult(mixStationData);
    }

    /**
     * 保存拌和站
     */
    @RequestMapping(value = "/saveMixStation", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveMixStation(@RequestParam(value = "result", required = false) String sresult, HttpServletRequest request,
                                    @RequestParam(value = "file", required = false) MultipartFile multipartFile,
                                    @RequestParam(value = "type") String type, String institution){
        if(StringUtils.isEmpty(type)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        int result = 0;
        try {
            Long fileId = null;
            if(multipartFile != null) {
                fileId = super.saveFile((CommonsMultipartFile) multipartFile);
            }
            result = mixBuildService.saveMixStation(userId, beamId, sresult, fileId, type, institution);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if (result == 0) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        } else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "保存成功");
        }
    }

    @RequestMapping(value = "/editManagerInfo", method = RequestMethod.GET)
    public ModelAndView editManagerInfo() {
        return new ModelAndView();
    }

    /**
     * 获取管理人员列表
     * @param offset
     * @param limit
     * @param name
     * @param identity
     * @return
     */
    @RequestMapping("/getMixManagerList")
    @ResponseBody
    public PageResult getMixManagerList(Integer offset, Integer limit, String identity, String name, HttpSession session, String  presasg){
        Long beam_id = (Long) session.getAttribute(beam);
        return labService.getManagerInfoList( offset,limit,identity,name,beam_id,"990110", null, presasg);
    }

    /**
     * 删除拌和站管理人员
     * @return
     */
    @RequestMapping("/delMixManager")
    @ResponseBody
    public ApiResult delMixManager(String id){
        if (StringUtils.isNotBlank(id)) {
            return mixBuildService.delMixManager(id);
        }else  {
            return new ApiResult("500","未获取删除人员");
        }
    }


    @RequestMapping(value = "/constr_inspection")
    public ModelAndView constr_inspection(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/in_acceptance")
    public ModelAndView in_acceptance(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/sup_acceptance")
    public ModelAndView sup_acceptance(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/owner_acceptance")
    public ModelAndView owner_acceptance(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/data_archive")
    public ModelAndView data_archive(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/comm_acceptance")
    public ModelAndView comm_acceptance(){
        return new ModelAndView();
    }

    @Autowired
    FileService fileService;

    /**
     * 获取文件
     * @return
     */
    @RequestMapping("/getFileInfoVo")
    @ResponseBody
    public FileInfoVo getFileInfoVo(Long file_id){
        return fileService.getFileInfoVo(file_id);
    }

    /**
     * 获取归档文件列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/getDataArchiveList",method = RequestMethod.GET)
    @ResponseBody
    public PageResult getDataArchiveList(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        return mixBuildService.getDataArchiveList(beamId);
    }
    /**
     * 删除拌归档资料
     * @return
     */
    @RequestMapping("/delDataArchive")
    @ResponseBody
    public ApiResult delDataArchive(String type, HttpServletRequest request){
        Long beamId = super.getBeamId(request);

        if (StringUtils.isNotBlank(type)) {
            return mixBuildService.delDataArchive(beamId,type);
        }else  {
            return new ApiResult("500","未获取删除的对应文件");
        }
    }
}
